/*
 * 查找最长回文子串
 * https://leetcode-cn.com/problems/longest-palindromic-substring/
 */

char * longestPalindrome(char * s){
    int slen = strlen(s);
    int left = 0, right = 0;
    int start = 0, end = 0;
    int cnt = 0;
    
    for (int i = 0; i < slen; i += cnt) {
        right = i + 1;
        left = i - 1;
        cnt = 1;
        while (right < slen && s[i] == s[right]) right++, cnt++;  // 如果中心有连续相等的字符
        while (left >= 0 && right < slen && s[left] == s[right]) {
            left--;
            right++;
        }
        left++;
        if (right - left > end - start) {
            start = left;
            end = right;
        }
    }
    s[end] = '\0';

    return s + start;
}
